package com.xwj.Handler;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by thinkjoy on 2017/7/12.
 * 统一异常处理
 */
@Slf4j
@ControllerAdvice
public class GlobalExceptionHandler {

    public static final String DEFAULT_ERROR_VIEW = "error/errorpage";

    @ExceptionHandler(value = Exception.class)
    public ModelAndView defaultErrorHandler(HttpServletRequest req, Exception e) throws Exception {
        ModelAndView mav = new ModelAndView();
        mav.addObject("name", e.getClass().getName());
        mav.addObject("exception", e.getMessage());
        mav.setViewName(DEFAULT_ERROR_VIEW);
        log.error("网页异常:{}\n", e.getClass().getName(), e);
        return mav;
    }
}
